package dk.hewison.client.panels;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import org.adamtacy.client.ui.NEffectPanel;
import org.adamtacy.client.ui.effects.NEffect;
import org.adamtacy.client.ui.effects.NEffectHandlerAdapter;
import org.adamtacy.client.ui.effects.impl.Fade;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 18:25:48 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 352 $:
 */
public class ImagePanel extends Composite {
    Image image;
    private Fade fadeIn;
    private Fade fadeOut;

    NEffectPanel effectPanel;
    private ImageStack imageStack;

    public ImagePanel(ImageStack imageStack, Image image) {
        this.imageStack = imageStack;
        this.image = image;

        init();

        initWidget(effectPanel);
    }

    private void init() {
        effectPanel = new NEffectPanel();
        //add(effectPanel);
        effectPanel.add(image);
        initEffect();

        effectPanel.addEffect(fadeIn);
        //effectPanel.setEffectsLength(0.3);
    }

    public void show() {
        effectPanel.playEffects();
    }

    public void hideImage() {
        fadeOut.play();
    }

    public void initEffect() {
        fadeIn = new MyFade();
        fadeIn.getProperties().setStartValue("0");
        fadeIn.getProperties().setEndValue("100");


        fadeOut = new MyFade();
        fadeOut.addEffectHandler(new NEffectHandlerAdapter() {
            @Override
            public void postEvent(NEffect theEffect) {
                doIsHidden();
            }
        });
        fadeOut.getProperties().setStartValue("100");
        fadeOut.getProperties().setEndValue("0");
        fadeOut.init(effectPanel);
    }

    private void doIsHidden() {
        imageStack.imageIsHidden();
    }

    class MyFade extends Fade {
        MyFade() {
            effectLengthSeconds = 0.2;
        }

    }
}
